package com.deltabravomedia.theoldtoad;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Food extends ListActivity {
	private static final int ACTIVITY_SPECIALS=0;
	private static final int ACTIVITY_STARTERS=1;
	private static final int ACTIVITY_SALADS=2;
	private static final int ACTIVITY_SANDWICHES=3;
	private static final int ACTIVITY_ENTREES=4;
	
	
	private static final String TAG = "Food";

	private ProgressDialog m_ProgressDialog = null; 
    private ArrayList<Item> m_items = null;
    private ItemAdapter m_adapter;
    private Runnable viewItems;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewwithbackgroundimage);
        this.setTitle("The Old Toad : Food");
        m_items = new ArrayList<Item>();
        this.m_adapter = new ItemAdapter(this, R.layout.row, m_items);
                setListAdapter(this.m_adapter);
        
        viewItems = new Runnable(){
            @Override
            public void run() {
                getItems();
            }
        };
        
        Thread thread =  new Thread(null, viewItems, "MagentoBackground");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(Food.this,    
              "Please wait...", "Retrieving data ...", true);
    }
    
	@Override
    protected void onListItemClick (ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        Log.i(TAG, m_items.get(position).getItemName());
        
        String item = m_items.get(position).getClassName();
        
        if (item.equals("FoodSpecials")) {
            Intent i = new Intent(this, FoodSpecials.class);
            startActivityForResult(i, ACTIVITY_SPECIALS);
        } else if (item.equals("FoodStarters")) {
            Intent i = new Intent(this, ItemsList.class);
            i.putExtra("title", "Food : Starters");
            i.putExtra("type", "Starters");
            startActivityForResult(i, ACTIVITY_STARTERS);
        } else if (item.equals("FoodSalads")) {
            Intent i = new Intent(this, ItemsList.class);
            i.putExtra("title", "Food : Salads");
            i.putExtra("type", "Salads");
            startActivityForResult(i, ACTIVITY_SALADS);
        } else if (item.equals("FoodSandwiches")) {
            Intent i = new Intent(this, ItemsList.class);
            i.putExtra("title", "Food : Sandwiches");
            i.putExtra("type", "Sandwiches");
            startActivityForResult(i, ACTIVITY_SANDWICHES);
        } else if (item.equals("FoodEntrees")) {
            Intent i = new Intent(this, ItemsList.class);
            i.putExtra("title", "Food : Entrees");
            i.putExtra("type", "Entrees");
            startActivityForResult(i, ACTIVITY_ENTREES);
        }
	}   
	
    private Runnable returnRes = new Runnable() {
        @Override
        public void run() {
            if(m_items != null && m_items.size() > 0){
                m_adapter.notifyDataSetChanged();
                for(int i=0;i<m_items.size();i++)
                m_adapter.add(m_items.get(i));
            }
            m_ProgressDialog.dismiss();
            m_adapter.notifyDataSetChanged();
        }
    };
    
    private class ItemAdapter extends ArrayAdapter<Item> {

        private ArrayList<Item> items;

        public ItemAdapter(Context context, int textViewResourceId, ArrayList<Item> items) {
	        super(context, textViewResourceId, items);
	        this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
			    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			    view = vi.inflate(R.layout.row, null);
			}
			Item item = items.get(position);
			if (item != null) {
			        TextView topText = (TextView) view.findViewById(R.id.toptext);
			        if (topText != null) {
			              topText.setText( item.getItemName());                            
			        }

			        ImageView iconView = (ImageView) view.findViewById(R.id.icon);
			        if (iconView != null) {
			        	int resID = getResources().getIdentifier(item.getIconName(), "drawable", "com.deltabravomedia.theoldtoad");
			            iconView.setImageResource(resID);                
			        }
			}
			return view;
        }
    }
    
    private void getItems(){
		m_items = new ArrayList<Item>();
		
		Item item0 = new Item();
		item0.setItemName("Specials");
		item0.setIconName("specials");
		item0.setClassName("FoodSpecials");
		
		Item item1 = new Item();
		item1.setItemName("Starters");
		item1.setIconName("starters");
		item1.setClassName("FoodStarters");
		
		Item item2 = new Item();
		item2.setItemName("Salads");
		item2.setIconName("salads");
		item2.setClassName("FoodSalads");
		
		Item item3 = new Item();
		item3.setItemName("Sandwiches");
		item3.setIconName("sandwiches");
		item3.setClassName("FoodSandwiches");
		
		Item item4 = new Item();
		item4.setItemName("Entrees");
		item4.setIconName("entrees");
		item4.setClassName("FoodEntrees");
		
		m_items.add(item0);
		m_items.add(item1);
		m_items.add(item2);
		m_items.add(item3);
		m_items.add(item4);
		
		Log.i("ARRAY", ""+ m_items.size());
		  
		runOnUiThread(returnRes);
    }
}